package com.alibaba.nacos.console.config;

import org.jasypt.encryption.StringEncryptor;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.stream.Stream;

/**
 * jasypt的配置
 *
 * @author huangchangjin
 * @date 2023/12/23 10:54
 */
@Component("jasyptStringEncryptor")
public class MyJasyptStringEncryptor implements StringEncryptor {
    @Override
    public String encrypt(String message) {
        return encryptPwd(message);
    }

    @Override
    public String decrypt(String message) {
        return decryptPwd(message);
    }

    private static String encryptPwd(String msg){
        return Base64.getEncoder().encodeToString(msg.getBytes(StandardCharsets.UTF_8));
    }

    private static String decryptPwd(String msg){
        return new String(Base64.getDecoder().decode(msg));
    }

    private static void test(String msg){
        String base64 = encryptPwd(msg);
        String msgNew = decryptPwd(base64);
        System.out.println("msgNew = " + msgNew + " base64 = " + base64);
    }

    public static void main(String[] args) {
        Stream.of("system","123456").forEach(MyJasyptStringEncryptor::test);
    }
}
